/**
 * Created by Administrator on 2015/1/26.
 */
define(function(require, exports){
    var $ = require('$'),

        myApp = new Framework7({
             pushStatue : true
            ,init : false
            ,router : false
            ,pushStateSeparator : '#!/'
            ,modalButtonOk : '确定'
            ,modalButtonCancel : '取消'
        }),

        $$ = $,

        mainView = myApp.addView('.view-main', {
            dynamicNavbar: false,
            domCache: true
        }),

        T = require('T');


    /**
     * 模拟浏览历史
     * 约束：后续页面 a 标签 不能有指向过往页面的链接，但是可以用api的方式:。
     * 返回 back
     * 首页 home ,将会情况 历史表
     */

    var nav = (function(){
        var list = {}, count = 0, hist_l = 0, hist = [], home_flag = false;

        var hash = [];

        window.addEventListener('hashchange' , function(){
            hist_l++;
        });


        function getPage(){
            return mainView.url.substring(1);
        }

        function go(page){
            if(hash.length == 0){
                hash.push(page);

                if(getPage() != page) {
                    if(home_flag) {
                        home_flag = false;
                        mainView.router.back({pageName: page , force : true});
                    }else{
                        mainView.router.load({pageName: page});

                    }
                }

                return;
            }

            if(getPage() == page) return;

            if(hash.length>=2 && hash[hash.length-2] == page){
                mainView.router.back({'pageName':page});
                //删除末尾
                hash.pop();

            }else{

                mainView.router.load({pageName: page});

                //追加到末尾
                hash.push(page);
            }
            console.log(hash)
        }

        function home(){console.log(hash.length)
            var l = hash.length - 1;
            hash = [];
            home_flag = true;
            window.history.go(-l);
        }

        function reset(){
            hash = [];
        }

        return {
            'go' : go,
            'home' : home,
            'reset' : reset,
            'hash' : function(v){
                location.hash = '!' + v;
            }
        };

    }());

    var pages = {};

    $(function(){
        $('[data-router]').each(function(){
            var $this = $(this),
                router = $this.data('router'),
                page = $this.data('page'),
                home = $this.data('home') !== undefined;
            pages[page] = {'router':router , 'home' : home};

            T.router.define(router , function(back){
                console.log(page);
                load(page);
            }, false);

        });

        // 订单列表页
        T.router.define('!/order/list/:id,!/order/list' , function(id){
            id = parseInt(id || 1) - 1;
            var page = '/order/list';
            pages[page] = {'router':'!/order/list/:id,!/order/list' , 'home' : false};
            load(page);
            $('#j-order-list .type a:eq('+id+')').addClass('j-select').siblings().removeClass('j-select');
            //console.log($('#j-order-list .type a:eq('+id+')'))
        })

        T.router.init();

        $('.j-home').on('click' , function () {

            nav.home();
        })

        $('#j-nav-user').on('click' , function(){
            nav.hash('/user/reset');
        })
    })

/*
    myApp.onPageInit('/index', function (page) {
        console.log('index page initialized');
        console.log(page);
    });
*/

    //myApp.init();
/*    $$(document).on('pageInit', function (e) {
        var name = $(e.target).attr('data-page');
        var page = getPage(name);
        //alert(page)
        console.log(page)
        // Do something here when page loaded and initialized
    })*/

    function getPage(page){
        for(var i in pages){
            if(i == page) return pages[i];
        }
        return undefined;
    }

    function load(router){
        var page;
        for(var i in pages)
        {
            if(i == router)
            {
                page = i;
            }
        }

        //nav(page);
        if(page) {

            nav.go(page);
            /*if(pages[page].home) mainView.router.back({'pageName':page});
            else mainView.router.load({pageName: page});*/
        }
        //return undefined;
    }


    return {
        'view' : mainView,
        'app' : myApp,
        'nav' : nav
    }
})